const mRequest = require('../goodsDetailsService.js');
const service = require('../../../../utils/utilService.js')
const favManager = require('../../../../utils/favManager.js')
const constant = require('../../../../utils/constant.js');
let app = getApp()
/**
 * 物品价钱、介绍 View
 */
Component({

  options: {
    // 在组件定义时的选项中启用多slot支持
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {
    // 运费
    freightData: {
      type: String,
      value: '',
    },
  },

  data: {
    goodsName: '',
    goodsDetailsText: '',
    goodsDetailsData: {},
    storyData: {},
    isMyCandyVisible: false,   // 我拥有的数量和还剩多少宝贝
    isPraiseNameVisible: false,// 名称收藏
    isSenCandyVisible: false,  // 快递费 
    relateId: '',              // 收藏id
    intentType: '',            // 页面类型
    isConlection: true,        // 是否收藏过
    introduceData: {}          // 介绍数据

  },

  methods: {
    initData: function () {
      // 趣咸介绍
      this.introduceWindow = this.selectComponent("#introduceWindow");
      // 获取规则信息
      this.showRule()
    },

    /**
     * 物品详情数据
     */
    initGoodsData: function (goodsData, itType) {
      this.setData({
        goodsDetailsData: goodsData,
        goodsName: goodsData.goodsListEntity.name,
        goodsDetailsText: goodsData.description,
        relateId: goodsData.goodsListEntity.id,
        intentType: itType
      })
      this.showCollection()
    },

    /**
     * 故事详情数据
     */
    initStoryData: function (storyData, itType) {
      this.setData({
        goodsName: storyData.storyTitle,
        goodsDetailsText: storyData.content,
        isMyCandyVisible: true,  // 我拥有的数量和还剩多少宝贝
        isPraiseNameVisible: true,// 收藏
        isSenCandyVisible: true,  // 快递费 
        relateId: storyData.id,
        intentType: itType
      })
      this.showCollection()
    },

    /**
    * 查询是否收藏
    * >0已收藏，否则为未收藏
    */
    showCollection: function () {
      var that = this
      var param = {
        userId: app.globalData.userId,
        relateId: this.data.relateId
      }
      mRequest.getCollection(param,
        function (data) {
          that.setData({
            isConlection: data > 0 ? true : false
          })
          console.log('获取是否收藏过  成功')
        })
    },

    /**
     * 收藏点击事件
     */
    clickCollection: function () {
      var that = this
      var newType = ''
      if (that.data.intentType == constant.STORY) {
        newType = 1
      } else {
        newType = 0
      }

      if (that.data.isConlection) {
        if (newType == 0) {
          // 取消收藏商品
          favManager.deleteFavRequest(that.data.relateId,
            function (data) {
              that.setData({
                isConlection: !that.data.isConlection
              })
            })
        } else {
          //  取消收藏故事
          service.deleteFavRequest(that.data.relateId, 1, 
          function (data) {
            that.setData({
              isConlection: !that.data.isConlection
            })
          })
        }
      } else {
        if (newType == 0) {
          // 收藏商品
          favManager.favRequest(that.data.relateId,
            function (data) {
              that.setData({
                isConlection: !that.data.isConlection
              })
            })
        } else {
          // 收藏故事
          service.favRequest(that.data.relateId, 1,
           function(data) {
            that.setData({
              isConlection: !that.data.isConlection
            })
          })
        }
      }

    },

    /**
     * 获取规则信息
     */
    showRule: function () {
      let that = this
      let params = {
        type: 0
      }
      mRequest.getRule(params,
        function (data) {
          that.setData({
            introduceData: data
          })
        })
    },

    /**
     * 查看规则
     */
    clickSendCandy: function () {
      this.introduceWindow.show()
    },

  }
})
